package cn.spring.rabbitmq.demo.controller.rabbitmq;

import cn.spring.rabbitmq.demo.rabbitmq.message.DemoMessage;
import cn.spring.rabbitmq.demo.rabbitmq.sender.MessageSender;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author tanyong
 * @Version RabbitmqDemoController v1.0.0 2025/9/12 14:58 $$
 */
@Slf4j
@RestController
@RequestMapping("/rabbitmq")
@Tag(name = "RabbitMQ消息接口", description = "提供RabbitMQ消息发送相关接口")
public class RabbitmqDemoController {

    @Resource
    private MessageSender messageSender;

    /**
     * 发送基本消息
     *
     * @param userId  用户ID
     * @param message 消息内容
     * @return 发送结果
     */
    @PostMapping("/send-basic-message")
    @Operation(summary = "发送基本消息", description = "向RabbitMQ发送基本消息")
    public String sendBasicMessage(
            @Parameter(description = "用户ID", required = true, name = "userId") @RequestParam("userId") Long userId,
            @Parameter(description = "消息内容", required = true, name = "message") @RequestParam("message") String message) {
        try {
            DemoMessage demoMessage = new DemoMessage();
            demoMessage.setUserId(userId);
            demoMessage.setMessage(message);
            demoMessage.setCreateTime(new Date());

            messageSender.basicSend(demoMessage);

            log.info("消息发送成功: userId={}, message={}", userId, message);
            return "消息发送成功";
        } catch (Exception e) {
            log.error("消息发送失败: userId={}, message={}", userId, message, e);
            return "消息发送失败: " + e.getMessage();
        }
    }


    /**
     * 发送广播消息
     *
     * @param userId  用户ID
     * @param message 消息内容
     * @return 发送结果
     */
    @PostMapping("/send-fanout-message")
    @Operation(summary = "发送广播消息", description = "向RabbitMQ发送广播消息")
    public String sendFanoutMessage(
            @Parameter(description = "用户ID", required = true, name = "userId") @RequestParam("userId") Long userId,
            @Parameter(description = "消息内容", required = true, name = "message") @RequestParam("message") String message
    ) {
        try {
            DemoMessage fanoutMessage = new DemoMessage();
            fanoutMessage.setUserId(userId);
            fanoutMessage.setMessage(message);
            fanoutMessage.setCreateTime(new Date());

            messageSender.fanoutSend(fanoutMessage);

            log.info("广播消息发送成功: userId={}, message={}", userId, message);
            return "广播消息发送成功";
        } catch (Exception e) {
            log.error("广播消息发送失败: userId={}, message={}", userId, message, e);
            return "广播消息发送失败: " + e.getMessage();
        }
    }

    /**
     * 发送Topic消息
     *
     * @param userId     用户ID
     * @param message    消息内容
     * @param routingKey 路由键
     * @return 发送结果
     */
    @PostMapping("/send-topic-message")
    @Operation(summary = "发送Topic消息", description = "向RabbitMQ发送Topic消息")
    public String sendTopicMessage(
            @Parameter(description = "用户ID", required = true, name = "userId") @RequestParam("userId") Long userId,
            @Parameter(description = "消息内容", required = true, name = "message") @RequestParam("message") String message,
            @Parameter(description = "路由键", required = true, name = "routingKey") @RequestParam("routingKey") String routingKey
    ) {
        try {
            DemoMessage topicMessage = new DemoMessage();
            topicMessage.setUserId(userId);
            topicMessage.setMessage(message);
            topicMessage.setCreateTime(new Date());

            messageSender.topicSend(topicMessage, routingKey);

            log.info("Topic消息发送成功: userId={}, message={}, routingKey={}", userId, message, routingKey);
            return "Topic消息发送成功";
        } catch (Exception e) {
            log.error("Topic消息发送失败: userId={}, message={}, routingKey={}", userId, message, routingKey, e);
            return "Topic消息发送失败: " + e.getMessage();
        }
    }

    /**
     * 发送延迟消息
     *
     * @param userId  用户ID
     * @param message 消息内容
     * @param delay   延迟时间（毫秒）
     * @return 发送结果
     */
    @PostMapping("/send-delay-message")
    @Operation(summary = "发送延迟消息", description = "向RabbitMQ发送延迟消息")
    public String sendDelayMessage(
            @Parameter(description = "用户ID", required = true, name = "userId") @RequestParam("userId") Long userId,
            @Parameter(description = "消息内容", required = true, name = "message") @RequestParam("message") String message,
            @Parameter(description = "延迟时间(毫秒)", required = true, name = "delay") @RequestParam("delay") Long delay
    ) {
        try {
            DemoMessage delayMessage = new DemoMessage();
            delayMessage.setUserId(userId);
            delayMessage.setMessage(message);
            delayMessage.setCreateTime(new Date());

            messageSender.delaySend(delayMessage, delay);

            log.info("延迟消息发送成功: userId={}, message={}, delay={}ms", userId, message, delay);
            return "延迟消息发送成功，将在" + delay + "毫秒后收到消息";
        } catch (Exception e) {
            log.error("延迟消息发送失败: userId={}, message={}, delay={}ms", userId, message, delay, e);
            return "延迟消息发送失败: " + e.getMessage();
        }
    }

    /**
     * 批量发送消息
     *
     * @param count 消息数量
     * @return 发送结果
     */
    @PostMapping("/send-batch-message")
    @Operation(summary = "批量发送消息", description = "向RabbitMQ批量发送消息")
    public String sendBatchMessage(
            @Parameter(description = "用户ID", required = true, name = "userId") @RequestParam("userId") Long userId,
            @Parameter(description = "基础消息内容", required = true, name = "message") @RequestParam("message") String message,
            @Parameter(description = "消息数量", required = true, name = "count") @RequestParam("count") Integer count
    ) {
        try {
            List<DemoMessage> messages = new ArrayList<>();
            for (int i = 0; i < count; i++) {
                DemoMessage demoMessage = new DemoMessage();
                demoMessage.setUserId(userId);
                demoMessage.setMessage(message + "-" + (i + 1));
                demoMessage.setCreateTime(new Date());
                messages.add(demoMessage);
            }

            messageSender.batchSend(messages);

            log.info("批量消息发送成功: userId={}, message={}, count={}", userId, message, count);
            return "批量消息发送成功，共发送 " + count + " 条消息";
        } catch (Exception e) {
            log.error("批量消息发送失败: userId={}, message={}, count={}", userId, message, count, e);
            return "批量消息发送失败: " + e.getMessage();
        }
    }
}
